<?
/*
* ===========================================*
* 函数说明:最小化的记录流量的内核
* ===========================================*
*/
class _stat
{

	var $_db; //数据列表
	var $time;
	var $year;
	var $month;
	var $day;
	var $week;
	var $hour;
	var $newDay = false;
	var $newViewer = true;
	var $viewerInfo = array();

	var $dayStartTime;
	var $guid;
	var $actions;
	var $actiontype;//操作类型

	/*
	* 函数说明:初始化
	*/
	function _stat($actiontype)
	{
		global $_db;
		$this->_db = $_db;//初始化数据列表
		$this->time = time();

		if($_GET['guid']!=''){
			$this->guid=$_GET['guid'];
		}else{
			$this->guid=$_COOKIE['guid'];
		}

		$this->actiontype=$actiontype;


		/*执行相应的统计操作*/
		if($actiontype=='_stat'){
			//流量统计
			$this->_do_count();
		}elseif($actiontype=='_click'){
			//点击统计
			$this->_do_click();
		}elseif($actiontype=='_ad'){
			//广告统计
			$this->_do_ad();
		}elseif($actiontype=='_biz'){
			//部分针对性统计
			$this->_do_biz();
		}else{
			exit();
		}

	}

	/*
	* 函数说明:流量统计初始化
	*/
	function _do_count()
	{

		$this->year = date( 'Y', $this->time );//4 位数字完整表示的年份
		$this->month = date( 'm', $this->time );//数字表示的月份，有前导零
		$this->day = date( 'j', $this->time );//月份中的第几天，没有前导零
		$this->week = date( 'w', $this->time );//星期中的第几天，数字表示
		$this->hour = date('G', $this->time );//小时，24 小时格式，没有前导零
		$this->dayStartTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
		$this->getViewerInfo();
		$this->saveViewerInfo();

	}

	/*
	* 函数说明:点击统计初始化
	*/
	function _do_click()
	{
		$this->time = time();
		$sql = "INSERT INTO " .$this->_db['click']. " (
		`guid` ,`pageurl` ,`type`,`screen_w` ,`click_x` ,`click_y` ,`uptime`
			) VALUES (
			'{$this->guid}',
			'{$_GET[pageid]}',
			'{$_GET[type]}',
			'{$_GET[w]}',
			'{$_GET[x]}',
			'{$_GET[y]}',
			'{$this->time}'
			 );";

		$this->_query( $sql );
	}

	/*
	* 函数说明:AD统计初始化
	*/
	function _do_ad()
	{
		/*
		$this->time = time();
		$sql = "INSERT INTO " .$this->_db['ad']. " (
		`guid` ,`pageurl` ,`type`,`screen_w` ,`click_x` ,`click_y` ,`uptime`
			) VALUES (
			'{$this->guid}',
			'{$_GET[pageid]}',
			'{$_GET[type]}',
			'{$_GET[w]}',
			'{$_GET[x]}',
			'{$_GET[y]}',
			'{$this->time}'
			 );";

		$this->_query( $sql );
		*/
	}

	/*
	* 函数说明:部分针对性统计
	*/
	function _do_biz()
	{
		/*
		$this->time = time();
		$sql = "INSERT INTO " .$this->_db['com']. " (
		`guid` ,`pageurl` ,`type`,`screen_w` ,`click_x` ,`click_y` ,`uptime`
			) VALUES (
			'{$this->guid}',
			'{$_GET[pageid]}',
			'{$_GET[type]}',
			'{$_GET[w]}',
			'{$_GET[x]}',
			'{$_GET[y]}',
			'{$this->time}'
			 );";

		$this->_query( $sql );
		*/
	}


	/*从JS传过来的时间转化为时间戳*/
	function getFLTime( $val )
	{
		$val = explode( '-', $val );
		return mktime ( $val[3], $val[4], $val[5], $val[1], $val[2], $val[0] );
	}

	/*获得浏览者信息*/
	function getViewerInfo()
	{
		$this->viewerInfo[time] = &$this->time;//时间
		$this->viewerInfo[page] = $_GET[pageurl];//当前URL
		$this->viewerInfo[referer] = $_GET[referer];//上一页
		$this->viewerInfo[language] = strtolower( $_GET[language] );//语言
		$this->viewerInfo[color] = $_GET[color];//色深
		$this->viewerInfo[screen] = $_GET[screensize];//屏幕尺寸
		$this->viewerInfo[http_user_agent] = $_SERVER["HTTP_USER_AGENT"];//浏览用户
		$this->viewerInfo[remote_addr] = $_SERVER["REMOTE_ADDR"];//IP地址
		$this->viewerInfo[timezone] = $_GET["timezone"];//时区
		$this->viewerInfo[firsttime] = $this->getFLTime($_GET["firsttime"] );//第一次访问时间
		$this->viewerInfo[lasttime] = $this->getFLTime($_GET["lasttime"] );//最后一次访问时间
		$this->viewerInfo['return'] = intval( $_GET['return1'] );//回访次数
		$this->viewerInfo['counturl'] = $_GET['counturl'];

		$this->viewerInfo[charset] = strtolower ($_GET["charset"] );//字符
		$this->viewerInfo[type] = intval( $_GET["type"] );


		//来自站点
		//简化的数据处理不需要记录关键字
		preg_match( "|(http://[^/]+?)/.*|isU", $this->viewerInfo[REFERER], $tmp );
		$this->viewerInfo[referer] = trim( $tmp[1] );

		//查找关键字
		//$this->viewerInfo[keyword] =GetKeyWord($this->viewerInfo[referer],$this->viewerInfo[REFERER]);


		//查找是否有alexa工具条
		if( strpos( $this->viewerInfo[http_user_agent], 'Alexa') )
		{
			$this->viewerInfo[alexatool] = 1;
		}
		else
		{
			$this->viewerInfo[alexatool] = 0;
		}

		//获取浏览器信息
		//简化的数据处理不需要记录操作系统和浏览器
		//$this->viewerInfo[browser]=GetBrowser($this->viewerInfo[http_user_agent]);
		//$this->viewerInfo[system]=GetSystem($this->viewerInfo[http_user_agent]);
		//_d($this->viewerInfo);

	}

	/*保存浏览者的信息*/
	function saveViewerInfo()
	{
		$sql = "INSERT INTO " .$this->_db['ip']. " (
		`guid`,`ip`, `time`, `pageurl`, `pagefrom`, `language`, `color`, `screensize`, `http_user_agent`, `pagefromsite`, `alexatool`, `timezone`, `firsttime`, `lasttime`,`todayfirst`, `return`
			) VALUES (
			'{$this->guid}',
			'{$this->viewerInfo[remote_addr]}',
			'{$this->viewerInfo[time]}',
			'{$this->viewerInfo[page]}',
			'{$this->viewerInfo[referer]}',
			'{$this->viewerInfo[language]}',
			'{$this->viewerInfo[color]}',
			'{$this->viewerInfo[screen]}',
			'{$this->viewerInfo[http_user_agent]}',
			'{$this->viewerInfo[referer]}',
			'{$this->viewerInfo[alexatool]}',
			'{$this->viewerInfo[timezone]}',
			'{$this->viewerInfo[firsttime]}',
			'{$this->viewerInfo[lasttime]}',
			'" . ( $this->newViewer == true ? 1 : 0 ) . "',
			'".$this->viewerInfo['return']."' );";

		$this->_query( $sql );
	}

	/*报错*/
	function _error( $msg )
	{
		exit( $msg );
	}

	/*运行SQL,如果出错则记录*/
	function _query( $sql, $d = false )
	{
		@mysql_query("SET NAMES UTF8");
		$res = @mysql_query( $sql );
		if( $d || $_GET[debug] == 'true'  )
		{
			echo $sql . "<br>\n";
			echo mysql_error();
		}
		if($res){
			return $res;
		}else{
			$f=@fopen('../Logs/stat_mysql_error.log','a+');
			@fwrite($f,date("Y-m-d H:i:s").mysql_error().'--'.$sql."\n");
			@fclose($f);
			return false;
		}
	}
}




?>